home *** CD-ROM | disk | FTP | other *** search
- Path: gryphon.phoenix.net!usenet
- From: brucew@phoenix.net (Bruce Wedding)
- Newsgroups: comp.lang.c
- Subject: Re: Newbie - I'm stuck
- Date: Mon, 29 Jan 1996 06:01:10 GMT
- Organization: BranPaul Systems
- Message-ID: <4ehlit$r21@gryphon.phoenix.net>
- References: <mhoward-2701961816570001@port6.sniff.smallmedia.com>
- NNTP-Posting-Host: dial21.phoenix.net
- X-Newsreader: Moe's Newsreader
-
- mhoward@plainfield.bypass.com (Mark Howard) wrote:
-
- > I've stepped through
- >this a bunch of times in the debugger but I'm still clueless!
-
- When you were in the debugger, did you have awatch on the value of
- cntr1?
-
-
- > char halfbyt[8];
- ^^^ See this 8 ?
-
- > while(bitCntr < 32)
- > {
- ^^^^ See this 32?
- > halfbyt[cntr1] = NULL;
- > for(cntr2 = 0; cntr2 < 4; cntr2 ++)
- > {
- > if((num & (1 << bitCntr)) != 0)
- > /* set corresponding bit in halfbyt[] */
- > halfbyt[cntr1] |= (1 << cntr2);
- ^^^^ See this used as
-
- an index in this size 8 array
- > bitCntr ++;
- > }
- > cntr1 ++;
- ^^^^^^^ See this increment 32 times
- > }
-
- Here is a fixed version in more ways then I've pointed out.
-
- #include <stdio.h>
-
- int main(void)
- {
- char line[20];
- unsigned long num; /* our long int to split */
- int i, j, bitCntr; /* counters */
- char halfbyt[8] = {0};
-
- printf("Enter a number: "); /* Get a num */
- fgets(line, sizeof(line), stdin);
- sscanf(line, "%lu", &num);
-
- printf("You Entered (decimal) --> %lu\n", num);
- printf("You Entered (hex) --> %#.8lx\n", num);
-
- i = 0;
- bitCntr = 0;
- for ( i = 0; i < 8; i++)
- {
- for(j = 0; j < 4; j ++)
- {
- if((num & (1L << bitCntr)))
- halfbyt[i] |= (1L << j);
- bitCntr++;
- }
- }
- printf("Split:\n");
- for(i = 0; i < 8; i ++)
- printf("Half Byte %d --> %#x\n", i, halfbyt[i]);
- return 0;
- }
-
- Bruce D. Wedding Have Compiler, Will Travel!
- Perspicacious Programming Performed Promptly
- Katy, Texas, USA, Planet Earth, Milkyway Galaxy, Known Universe
-
-